//
//  SYCircleProgressView.m
//  miniwangdai
//
//  Created by Steven.Yung on 16/6/13.
//  Copyright © 2016年 容贤郁. All rights reserved.
//

#import "SYCircleProgressView.h"

#define degreesToRadians(x) (M_PI*(x)/180.0)

#define PROGRESS_LINE_WIDTH 5

#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a];
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface SYCircleProgressView ()

@property (strong, nonatomic) CAShapeLayer *progressLayer;

@end

@implementation SYCircleProgressView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super initWithCoder:aDecoder];
    
    if (self) {
        
    }
    
    return self;
}

//- (void)setProgressColor:(UIColor *)progressColor {
//    _progressColor = progressColor;
//    
//    //从顶部开始绘制，角度为-90~270
//    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 startAngle:degreesToRadians(-90) endAngle:degreesToRadians(270) clockwise:YES];
//    
//    _trackLayer.path = [path CGPath];
//    
//    _progressLayer = [CAShapeLayer layer];
//    _progressLayer.frame = self.bounds;
//    _progressLayer.fillColor = [[UIColor clearColor] CGColor];
//    _progressLayer.strokeColor = self.progressColor.CGColor;//这个一定不能用clearColor，然显示不出来
//    _progressLayer.lineCap = kCALineCapButt;
//    _progressLayer.lineWidth = PROGRESS_LINE_WIDTH;
//    _progressLayer.path = [path CGPath];
//    _progressLayer.strokeEnd = 0.0;
//    
//    [self.layer addSublayer:_progressLayer];
//}

- (void)progressPercent:(CGFloat)percent withColor:(UIColor *)color animated:(BOOL)animated {
    self.progressColor = color;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 startAngle:degreesToRadians(-90) endAngle:degreesToRadians(270) clockwise:YES];
    
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.bounds;
    _progressLayer.fillColor = [[UIColor clearColor] CGColor];
    _progressLayer.strokeColor = color.CGColor;//这个一定不能用clearColor，然显示不出来
    _progressLayer.lineCap = kCALineCapRound;
//    _progressLayer.lineJoin = kCALineJoinMiter;
    _progressLayer.lineWidth = PROGRESS_LINE_WIDTH;
    _progressLayer.path = [path CGPath];
    [self.layer addSublayer:_progressLayer];
    
    if (animated) {
        // 创建Animation
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        animation.fromValue = @(0.0);
        animation.toValue = @(percent/100.0);
        _progressLayer.autoreverses = NO;
        animation.duration = 1.0;
        
        // 设置layer的animation
        [_progressLayer addAnimation:animation forKey:nil];
        
        animation.delegate = self;
        
    } else {
        _progressLayer.strokeEnd = percent/100.0;
    }
//    _progressLayer.lineCap = kCALineCapButt;
}

//animation结束回调
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    NSLog(@"yes");
    _progressLayer.fillColor = self.progressColor.CGColor;
}

@end
